package com.video.zlc.audioplayer;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.video.zlc.audioplayer.ui.BaseActivity;
import com.video.zlc.audioplayer.ui.VoiceActivity;

public class MainActivity extends BaseActivity {

    private TextView id_tv_record;
    private TextView id_tv_listen;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        initView();
        initListener();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        super.initView();

        Log.e("init===1", "===");

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            int checkPermission = this.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION);
            if (checkPermission != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},101);
                return;
            }
        }

        Log.e("init===2", "===");


        // 读写手机状态和身份
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            int checkPermission = this.checkSelfPermission(Manifest.permission.READ_PHONE_STATE);
            if (checkPermission != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE}, 100);
                return;
            }
        }

        Log.e("init===3", "===");

        // 写入扩展存储，向扩展卡写入数据，用于写入离线定位数据
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            int checkPermission = this.checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE);
            if (checkPermission != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
                return;
            }
        }

        Log.e("init===4", "===");

        if (Build.VERSION.SDK_INT >= 23) {
            int checkPermission = checkSelfPermission(Manifest.permission.CAMERA);
            if (checkPermission != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(new String[]{Manifest.permission.CAMERA}, 102);
                return;
            }
        }

        Log.e("init===5", "===");

        id_tv_record = findView(R.id.id_tv_record);
        id_tv_listen = findView(R.id.id_tv_listen);
        ImageView id_iv_fanhui = findView(R.id.id_iv_fanhui);
        id_iv_fanhui.setVisibility(View.GONE);
    }

    @Override
    protected void initListener() {
        super.initListener();
        id_tv_record.setOnClickListener(this);
        id_tv_listen.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){
            case R.id.id_tv_listen:
                startActivity(VoiceActivity.class,"from","listen");
                break;
            case R.id.id_tv_record:
                startActivity(VoiceActivity.class,"from","record");
                break;
        }
    }
}
